using Glpt.Data;
using System.Data;

namespace Glpt.Task
{
    /// <summary>
    /// 解析加密的JSON
    /// </summary>
    public class ParseAESJSON
    {
        /// <summary>
        /// 参数设置
        /// </summary>
        /// <param name="db_no">数据库编号</param>
        /// <param name="tradedate">业务日期</param>
        /// <param name="sql">SQL语句</param>
        /// <returns></returns>
        public DataTableReader SetParams(string db_no, string tradedate, string sql)
        {
            return SetParams(db_no, tradedate, sql, "", "", "", "");
        }

        /// <summary>
        /// 参数设置
        /// </summary>
        /// <param name="db_no">数据库编号</param>
        /// <param name="tradedate">业务日期</param>
        /// <param name="sql">SQL语句</param>
        /// <param name="table_name">表名</param>
        /// <returns></returns>
        public DataTableReader SetParams(string db_no, string tradedate, string sql, string table_name)
        {
            return SetParams(db_no, tradedate, sql, table_name, "", "", "");
        }

        /// <summary>
        /// 参数设置
        /// </summary>
        /// <param name="db_no">数据库编号</param>
        /// <param name="tradedate">业务日期</param>
        /// <param name="sql">SQL语句</param>
        /// <param name="table_name">表名</param>
        /// <param name="field_name">json字段名</param>
        /// <returns></returns>
        public DataTableReader SetParams(string db_no, string tradedate, string sql, string table_name, string field_name)
        {
            return SetParams(db_no, tradedate, sql, table_name, field_name, "", "");
        }

        /// <summary>
        /// 参数设置
        /// </summary>
        /// <param name="db_no">数据库编号</param>
        /// <param name="tradedate">业务日期</param>
        /// <param name="sql">SQL语句</param>
        /// <param name="table_name">表名</param>
        /// <param name="field_name">json字段名</param>
        /// <param name="crypt_pwd">解密密码</param>
        /// <param name="crypt_iv">解密密钥</param>
        /// <returns></returns>
        public DataTableReader SetParams(string db_no, string tradedate, string sql, string table_name, string field_name, string crypt_pwd, string crypt_iv)
        {
            BaseInterFace bif = new BaseInterFace(int.Parse(db_no));
            DataTable dtlist = bif.ExecSelect(sql);
            DataTable dtresult = null;
            foreach (DataRow dr in dtlist.Rows)
            {
                string CONTENT = dr[dtlist.Columns.Count - 1].ToString();
                CONTENT = (crypt_pwd != null && crypt_pwd.Length > 0) ? AEScrypt.AESDecrypt(CONTENT, crypt_pwd, crypt_iv) : CONTENT;
                JsonDataTable jdt = new JsonDataTable(CONTENT, table_name, field_name);
                DataTable srcdt = jdt.GetDataTable();
                for (int i = 0; i < dtlist.Columns.Count - 1; i++)
                {
                    string colname = dtlist.Columns[i].ColumnName;
                    srcdt.Columns.Add(colname);
                    foreach (DataRow dr2 in srcdt.Rows)
                        dr2[colname] = dr[i];
                }
                if (dtresult == null)
                    dtresult = srcdt;
                else
                    dtresult.Merge(srcdt);
            }
            if (dtresult == null) dtresult = new DataTable();
            return dtresult.CreateDataReader();
        }
    }
}

